package xyz.huangziheng.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import xyz.huangziheng.basic.util.PageList;
import xyz.huangziheng.entity.Department;
import xyz.huangziheng.mapper.DepartmentMapper;
import xyz.huangziheng.service.DepartmentService;
import xyz.huangziheng.utli.DepartmentPage;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }

    @Override
    public Department findById(Long id) {
        return departmentMapper.findById(id);
    }

    @Override
    @Transactional
    public boolean getDelete(Long id) {
        if (id != null) {
            departmentMapper.getDelete(id);
            return true;
        }

        return false;
    }

    @Override
    @Transactional
    public boolean getUpdate(Department department) {
        if (department.getId() != null) {
            departmentMapper.getUpdate(department);
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean getInset(Department department) {
        if (department != null) {
            departmentMapper.getInset(department);
            return true;
        }
        return false;
    }

    @Override
    public PageList<Department> getPagination(DepartmentPage page) {
        PageList<Department> pageList = new PageList<>();
        if (page.getCurrentPage() != null || page.getCurrentPage() > 0) {
            Integer totalNumber = departmentMapper.getTotalNumber(page);
            pageList.setTotals(totalNumber);
            pageList.setRows(departmentMapper.getPagination(page));
            return pageList;
        }
        return new PageList<>(0, new ArrayList<>());
    }
}
